// pages/member-manage/member-manage.js
var request = require('../../utils/request.js');
var config = require('../../config.js');

Page({
    /**
     * 页面的初始数据
     */
    data: {
        haveMemberData:true, //有会员
        noInternet:false,    //请求错误
        default_user_img: '../../images/ic-touxiang-moren.png',
        scroll: {
            height: 0
        },
        searchP:{
            page_size:10,
            p:1,
            keyword :'',
            isOver : false
        },
        searchImg: '../../images/ic-search-black.png',
        topImg: '../../images/to-top.png',
        cartGreen: '../../images/cart-green.png',
        cartGray: '../../images/cart-gray.png',
        list: []
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.autoWindowHeight();
    },

    clickTurn: function (e) {
        var that = this;
        var turn = parseInt(e.currentTarget.id);
        var length = that.data.list.length;
        for (var i = 0; i < length; i++) {
            var check = 'list[' + i + '].checked';
            if (turn == i) {
                if (that.data.list[i].checked == true) {
                    that.setData({
                        [check]: false
                    })
                } else {
                    that.setData({
                        [check]: true
                    })
                }
            }
        }
    },

    search: function (e) {
        var that = this;
        that.submitAjax(e.detail.value)
    },
    submitAjax(data) {
        var that = this;
       
        request.post({'path': config.memberManage}, data).then(function (res) {
     
            var data = res.data.data;
            var arr = [];
            if (res.data.ret == 0) {
            
              if (data.list.length == 0){ // 数据为空的时候
                that.setData({
                  memberBlank:true,
                  haveMemberData: false,
                  noInternet: false
                })
                return false;
              }
                for (var i = 0; i < data.list.length; i++) {
                    if (Array.isArray(data.list[i].cars)) {
                        for (var j = 0; j < data.list[i].cars.length; j++) {
                            console.log(data.list[i].cars[j], '-----');
                            arr.push(data.list[i].cars[j].license_number);
                            data.list[i].carNum = arr.join();
                        }
                    }
                }
                // console.log(data.list)
                that.setData({
                    list: data.list
                });
            }else{
              //请求出错的时候
               that.setData({
                 memberBlank: false, 
                 haveMemberData: false,
                 noInternet: true
               })
            }
        })
    },

    // 获取高度和赋值
    autoWindowHeight(){
        wx.getSystemInfo({
            success: (res) => {
                this.setData({
                    'scroll.height': res.windowHeight
                })
            }
        })
    },
    inputKey: function (e) {
        let that = this;
        let lsData = that.data;
        let name = e.currentTarget.dataset.name;
        let nameArr = name.split(".");
        let newVal = e.detail.value;
        let nameArrLen = nameArr.length
        for (let i = 0; i <= nameArrLen; i++) {
            let val = lsData[nameArr[i]];
            if (nameArrLen - 1 === i) {
                lsData[nameArr[i]] = newVal;
            } else {
                lsData = lsData[nameArr[i]];
            }
        }
        this.setData({
            submitP: that.data.submitP
        })
        // debugger
    },


    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        this.tapSearch();
    },
    tapSearch(){
        let that = this;
        let keyword = that.data.searchP.keyword;
        that.setData({
            'searchP.keyword': keyword
        });
        that.submitAjax(true);
    },
    search: function(e) {
        let that = this;
        let keyword = that.data.searchP.keyword;
        that.setData({
            'searchP.keyword': keyword
        });
        that.submitAjax(true);
    },
    submitAjax(isInit){
        let that = this;
        let searchP = that.data.searchP;
        if(isInit){
            searchP.p = 1;
        }else{
            // 如果为结束值，不在发送请求
            if(searchP.isOver){
                return false;
            }
        }
        request.post({ 'path': config.memberManage }, searchP).then(function (res) {
            if (res.data.ret == 0) {
                let data = res.data.data;
                let nowList = that.data.list;
                if (data.list.length == 0) { // 数据为空的时候
                  that.setData({
                    haveMemberData: false,
                    noInternet: false
                  })
                  that.autoWindowHeight();
                  return false;
                }else{
                  that.setData({
                    haveMemberData: true,
                    noInternet: false
                  })
                }
                // for (var i = 0; i < data.list.length; i++) {
                //     if (Array.isArray(data.list[i].cars)) {
                //         for (var j = 0; j < data.list[i].cars.length; j++) {
                //             console.log(data.list[i].cars[j], '-----');
                //             data.list[i].carNum = arr.join();
                //         }
                //     }
                // }

                //遍历数据
                if(searchP.p === 1){
                    nowList = data.list;
                }else{
                    nowList.push(...data.list);
                }
                that.setData({
                    list: nowList
                });

                //p的临界点
                if(searchP.p >= data.pagination.total_page){
                    searchP.isOver = true
                }else{
                    searchP.isOver = false
                }
                //p加1
                searchP.p++

                that.setData({
                    'searchP': searchP
                });
                that.autoWindowHeight();
            }else{
              //请求出错的时候
              that.setData({
                haveMemberData: false,
                noInternet: true
              })
            }
        })
    },
    loadMore(){
        this.submitAjax()
    },
    goBackIndex(){
      wx.navigateBack({
        delta: 1
      })
    },
    tapReLoad(){
      var that = this;
      that.submitAjax(true);
    }
})